package com.ipt.frm.pw;

import java.io.IOException;
import java.text.MessageFormat;
import java.util.Properties;
import java.util.ResourceBundle;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ipt.frm.pw.Enumeraciones.TipoMensajes;
import com.ipt.frm.pw.beans.Mensaje;

/**
 * Servlet implementation class LoginServlet
 */
public final class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	//Mensaje enviado por request para mostrar al usuario
	static final String MSG = "MSG";
	static Properties propertiesMsg;
	
	//Parametros de inicio del Servlet
    static final String INIT_PARAM_USER_NAME = "username";
    static final String INIT_PARAM_USER_PASS = "userpass";
    
    //Parametros de la request del login.jsp
    static final String LOGIN_JSP_PARAM_USER_NAME = "user";
    static final String LOGIN_JSP_PARAM_USER_PASS = "password";
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
       
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		RequestDispatcher dispatcher = null;
		HttpSession session = request.getSession();
		//Recoger valores de la request
		String loginUser = request.getParameter(LOGIN_JSP_PARAM_USER_NAME);
		String loginPass = request.getParameter(LOGIN_JSP_PARAM_USER_PASS);
		propertiesMsg= (Properties) getServletContext().getAttribute("propertiesMsg");
			
		//Recoger parametros de inicio y compararlos con los de la jsp
		if(	getInitParameter(INIT_PARAM_USER_NAME).equals(loginUser) && 
			getInitParameter(INIT_PARAM_USER_PASS).equals(loginPass)){
			
			//Si datos validados ---> guardar en session + index.jsp 
			session.setAttribute(INIT_PARAM_USER_NAME, loginUser);
			session.setAttribute(INIT_PARAM_USER_PASS, loginPass);
			//String[] result = request.getParameter("uri").split("/");
			//forward index.jsp
			//dispatcher = request.getRequestDispatcher(result[result.length-1]);
			dispatcher = request.getRequestDispatcher("index.jsp");
			Mensaje mensaje= new Mensaje(TipoMensajes.EXITO,MessageFormat.format(propertiesMsg.getProperty("login.success"),session.getAttribute(INIT_PARAM_USER_NAME)));
			request.setAttribute(MSG,mensaje);
			
			request.setAttribute("title","Index");
		}else{
			
			//si no valido login.jsp
			dispatcher = request.getRequestDispatcher("login.jsp");
			
			//msg error
			//TODO meter estos literales en un fichero .properties
			request.setAttribute(MSG,new Mensaje(TipoMensajes.ERROR,propertiesMsg.getProperty("login.error")));
			request.setAttribute("title","Login");
		}
		dispatcher.forward(request, response);
		
	}

}
